<template>
  <div class="post-detail-page" v-if="post">
    <!-- 文章头部区域 - 增加渐变背景 -->
    <div class="post-header-section">
      <div class="container">
        <header class="post-header">
          <h1 class="post-title" v-text="post.title"></h1>
          <div class="post-meta">
            <span class="post-date" v-text="formatDate(post.date)"></span>
            <span class="post-category" v-text="post.category"></span>
            <span class="post-author">作者: <span v-text="post.author"></span></span>
          </div>
          <div class="post-tags">
            <span 
              v-for="tag in post.tags" 
              :key="tag" 
              class="tag"
              v-text="tag"
            >
            </span>
          </div>
        </header>
      </div>
    </div>
    
    <div class="container">
      <div class="post-main">
        <!-- 文章内容区域 - 增加卡片阴影和内边距 -->
        <article class="post-content">
          <div class="post-body" v-html="renderMarkdown(post.content)"></div>
        </article>
        
        <!-- 侧边栏 - 位置调整 -->
        <aside class="post-sidebar">
        <div class="sidebar-section">
          <h3>作者信息</h3>
          <div class="author-card">
            <div class="author-avatar">
              <img src="https://picsum.photos/seed/author/100/100" alt="作者头像">
            </div>
            <div class="author-info">
              <h4 v-text="post.author"></h4>
              <p>资深前端开发工程师，专注于Vue和React技术栈。</p>
            </div>
          </div>
        </div>
        
        <div class="sidebar-section">
          <h3>相关文章</h3>
          <div class="related-posts">
            <div 
              v-for="relatedPost in relatedPosts" 
              :key="relatedPost.id" 
              class="related-post"
            >
              <router-link :to="`/post/${relatedPost.id}`" class="related-post-link" v-text="relatedPost.title">
              </router-link>
              <span class="related-post-date" v-text="formatDate(relatedPost.date)"></span>
            </div>
          </div>
        </div>
      </aside>
    </div>
    
    <section class="comments-section">
      <div class="container">
        <h2 class="section-title">评论 (<span v-text="comments.length"></span>)</h2>
        
        <form @submit.prevent="addComment" class="comment-form">
          <div class="form-group">
            <input 
              v-model="newComment.author" 
              type="text" 
              placeholder="你的名字" 
              class="form-input"
              required
            />
          </div>
          <div class="form-group">
            <textarea 
              v-model="newComment.content" 
              placeholder="写下你的评论..." 
              class="form-textarea"
              rows="4"
              required
            ></textarea>
          </div>
          <button type="submit" class="submit-button">发表评论</button>
        </form>
        
        <div class="comments-list">
          <div 
            v-for="comment in comments" 
            :key="comment.id" 
            class="comment"
          >
            <div class="comment-header">
              <span class="comment-author" v-text="comment.author"></span>
              <span class="comment-date" v-text="formatDate(comment.date)"></span>
            </div>
            <div class="comment-content" v-text="comment.content">
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
  </div>
  
  <div class="post-not-found" v-else>
    <div class="container">
      <h1>文章未找到</h1>
      <p>抱歉，您访问的文章不存在。</p>
      <router-link to="/posts" class="back-link">返回文章列表</router-link>
    </div>
  </div>
</template>

<script>
import { renderMarkdown } from '@/utils/markdownUtils.js'
import mockData from '@/utils/mock.js'

export default {
  name: 'PostDetailPage',
  props: {
    id: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      post: null,
      relatedPosts: [],
      comments: [],
      newComment: {
        author: '',
        content: ''
      }
    }
  },
  mounted() {
    this.loadPost()
  },
  methods: {
    renderMarkdown,
    loadPost() {
      // 添加调试日志
      console.log('接收到的id参数:', this.id, '类型:', typeof this.id)
      // 从mock.js加载模拟数据
      this.post = mockData.getPostById(this.id)
      console.log('获取到的文章数据:', this.post)
      this.relatedPosts = mockData.getRelatedPosts()
      this.comments = mockData.getComments()
    },
    formatDate(dateString) {
      const options = { year: 'numeric', month: 'long', day: 'numeric' }
      return new Date(dateString).toLocaleDateString('zh-CN', options)
    },
    addComment() {
      if (this.newComment.author && this.newComment.content) {
        const comment = {
          id: this.comments.length + 1,
          author: this.newComment.author,
          content: this.newComment.content,
          date: new Date().toISOString().split('T')[0]
        }
        
        this.comments.unshift(comment)
        this.newComment = { author: '', content: '' }
      }
    }
  }
}
</script>

<style scoped>
/* 整体布局优化 */
.post-detail-page {
  width: 100%;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 文章头部区域 - 新增带渐变背景的头部区域 */
.post-header-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 4rem 0;
  margin-bottom: 2rem;
}

/* 文章主区域 - 新增主区域容器 */
.post-main {
  display: grid;
  grid-template-columns: 3fr 1fr;
  gap: 2rem;
  margin-bottom: 3rem;
  position: relative;
  top: -3rem;
}

/* 文章内容卡片 - 增加阴影和圆角 */
.post-content {
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 10;
  position: relative;
}

/* 文章标题样式优化 */
.post-header {
  text-align: center;
}

.post-title {
  font-size: 2.8rem;
  color: white;
  margin-bottom: 1.5rem;
  line-height: 1.3;
  font-weight: 700;
  position: relative;
  padding-bottom: 1.5rem;
}

.post-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 5px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 3px;
}

/* 元信息和标签样式优化 */
.post-meta {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
  color: rgba(255, 255, 255, 0.9);
  font-size: 1rem;
  font-weight: 500;
}

.post-category {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  padding: 0.4rem 0.8rem;
  border-radius: 20px;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.post-tags {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.7rem;
}

.tag {
  background: rgba(255, 255, 255, 0.15);
  color: white;
  padding: 0.4rem 1rem;
  border-radius: 20px;
  font-size: 0.9rem;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.tag:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* Markdown 样式 */
.post-body {
  line-height: 1.8;
  color: #444;
  font-size: 1.05rem;
  padding: 2rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
}

.post-body h1 {
  font-size: 2.2rem;
  margin: 2.5rem 0 1.5rem 0;
  color: #2c3e50;
  font-weight: 700;
}

.post-body h2 {
  font-size: 1.8rem;
  margin: 2rem 0 1.2rem 0;
  color: #34495e;
  position: relative;
  padding-bottom: 0.7rem;
  font-weight: 600;
}

.post-body h2::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 60px;
  height: 4px;
  background: linear-gradient(to right, #667eea, #764ba2);
  border-radius: 2px;
}

.post-body h3 {
  font-size: 1.4rem;
  margin: 1.8rem 0 1rem 0;
  color: #34495e;
  font-weight: 600;
}

.post-body p {
  margin-bottom: 1.5rem;
  text-align: justify;
}

.post-body p strong {
  font-weight: 600;
  color: #2c3e50;
}

.post-body p em {
  font-style: italic;
  color: #666;
}

.post-body ul, .post-body ol {
  margin-bottom: 1.5rem;
  padding-left: 1.5rem;
}

.post-body ul {
  list-style-type: none;
}

.post-body ul li {
  margin-bottom: 0.8rem;
  position: relative;
  padding-left: 1.5rem;
}

.post-body ul li::before {
  content: '•';
  color: #667eea;
  font-weight: bold;
  position: absolute;
  left: 0;
  font-size: 1.2rem;
}

.post-body ol {
  list-style-type: decimal;
}

.post-body ol li {
  margin-bottom: 0.8rem;
  padding-left: 0.5rem;
}

.post-body pre {
  background: #2d3748;
  color: #e2e8f0;
  padding: 1.5rem;
  border-radius: 12px;
  overflow-x: auto;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  font-family: 'Fira Code', 'Courier New', monospace;
}

.post-body code {
  font-family: 'Fira Code', 'Courier New', monospace;
  font-size: 0.9rem;
  background: #f1f5f9;
  color: #e53e3e;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

.post-body pre code {
  background: transparent;
  color: inherit;
  padding: 0;
  border-radius: 0;
}

.post-body a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  border-bottom: 2px solid rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.post-body a:hover {
  color: #764ba2;
  border-bottom-color: #764ba2;
}

.post-body blockquote {
  border-left: 4px solid #667eea;
  padding: 0.5rem 1.5rem;
  margin: 1.5rem 0;
  background: #f8f9fa;
  border-radius: 0 8px 8px 0;
  color: #666;
  font-style: italic;
}

.post-body img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 1.5rem 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.post-body img:hover {
  transform: scale(1.02);
}

/* 响应式调整 */
@media (max-width: 768px) {
  .post-body {
    padding: 1.5rem;
    font-size: 1rem;
  }
  
  .post-body h1 {
    font-size: 1.8rem;
  }
  
  .post-body h2 {
    font-size: 1.5rem;
  }
  
  .post-body h3 {
    font-size: 1.3rem;
  }
}

/* 侧边栏样式优化 */
.post-sidebar {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  height: fit-content;
  position: sticky;
  top: 2rem;
  z-index: 10;
}

.sidebar-section {
  margin-bottom: 2rem;
}

.sidebar-section:last-child {
  margin-bottom: 0;
}

.sidebar-section h3 {
  margin-bottom: 1rem;
  color: #333;
  font-size: 1.2rem;
  position: relative;
  padding-bottom: 0.5rem;
}

.sidebar-section h3::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 30px;
  height: 3px;
  background: linear-gradient(to right, #667eea, #764ba2);
  border-radius: 2px;
}

.author-card {
  display: flex;
  gap: 1rem;
  padding: 1rem;
  border-radius: 12px;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease;
}

.author-card:hover {
  transform: translateY(-3px);
}

.author-avatar img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #667eea;
}

.author-info h4 {
  margin: 0 0 0.5rem 0;
  color: #333;
}

.author-info p {
  margin: 0;
  color: #666;
  font-size: 0.9rem;
  line-height: 1.5;
}

.related-posts {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.related-post-link {
  text-decoration: none;
  color: #667eea;
  font-weight: 600;
  transition: all 0.3s;
  padding: 0.8rem;
  border-radius: 8px;
  background: white;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
  display: block;
}

.related-post-link:hover {
  color: #764ba2;
  transform: translateX(5px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.related-post-date {
  display: block;
  font-size: 0.8rem;
  color: #999;
  margin-top: 0.2rem;
}

/* Comments Section */
.comments-section {
  background: linear-gradient(to bottom right, #f8f9fa, #e9ecef);
  padding: 3rem 0;
  border-top: none;
}

.section-title {
  font-size: 1.8rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
  position: relative;
  padding-bottom: 1rem;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 4px;
  background: linear-gradient(to right, #667eea, #764ba2);
  border-radius: 2px;
}

.comment-form {
  background: white;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  margin-bottom: 3rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-input, .form-textarea {
  width: 100%;
  padding: 0.8rem;
  border: 2px solid #e1e5ea;
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  transition: all 0.3s;
}

.form-input:focus, .form-textarea:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-textarea {
  resize: vertical;
  min-height: 100px;
}

.submit-button {
  background: linear-gradient(to right, #667eea, #764ba2);
  color: white;
  padding: 0.8rem 2rem;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.3s;
  box-shadow: 0 4px 10px rgba(102, 126, 234, 0.3);
}

.submit-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(102, 126, 234, 0.4);
}

.submit-button:active {
  transform: translateY(0);
}

.comments-list {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.comment {
  background: white;
  padding: 1.5rem;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease;
}

.comment:hover {
  transform: translateY(-3px);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.comment-author {
  font-weight: 600;
  color: #333;
}

.comment-date {
  color: #999;
}

.comment-content {
  color: #555;
  line-height: 1.6;
}

/* Not Found */
.post-not-found {
  text-align: center;
  padding: 4rem 0;
}

.post-not-found h1 {
  font-size: 2rem;
  color: #333;
  margin-bottom: 1rem;
}

.post-not-found p {
  color: #666;
  margin-bottom: 2rem;
}

.back-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s;
  padding: 0.8rem 1.5rem;
  border: 2px solid #667eea;
  border-radius: 30px;
}

.back-link:hover {
  background: linear-gradient(to right, #667eea, #764ba2);
  color: white;
  border-color: transparent;
}

/* 响应式设计优化 */
@media (max-width: 1024px) {
  .post-main {
    grid-template-columns: 2fr 1fr;
  }
}

@media (max-width: 768px) {
  .post-main {
    grid-template-columns: 1fr;
    top: -2rem;
    gap: 1.5rem;
  }
  
  .post-header-section {
    padding: 3rem 0;
  }
  
  .post-title {
    font-size: 2rem;
  }
  
  .post-meta {
    flex-direction: column;
    gap: 0.8rem;
  }
  
  .comment-form {
    padding: 1.5rem;
  }
  
  .post-sidebar {
    position: relative;
    top: 0;
  }
}
</style>